﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using FrameWork.Setting;

namespace ServerUI
{
    public partial class SettingWnd : Form
    {
        public SettingWnd()
        {
            InitializeComponent();
        }
        private TreeNode[] FindTreeNode(string Text,TreeNodeCollection Tnodes)
        {
            List<TreeNode> tnodes = new List<TreeNode>();
            foreach(TreeNode tnode in Tnodes)
            {
                if(tnode.Text==Text)
                {
                    tnodes.Add(tnode);
                }
            }
            return tnodes.ToArray();
        }
        /// <summary>
        /// 初始化设定列表
        /// </summary>
        public void InitSettingList()
        {
            foreach (SettingEditorItem item in SettingPool.GetEditorItems())
            {
                string[] SettingPoints = item.GetShowName().Split('\\');
                int FindLevel = 0;
                TreeNode NowTreeNode = null;
                TreeNode[] Tnodes = FindTreeNode(SettingPoints[0], TViewSettingList.Nodes);
                if (Tnodes.Count() > 0)
                {
                    NowTreeNode = Tnodes[0];
                    FindLevel++;
                }
                while (NowTreeNode != null && FindLevel <= SettingPoints.Count())
                {
                    Tnodes = FindTreeNode(SettingPoints[FindLevel], NowTreeNode.Nodes);
                    if (Tnodes.Count() > 0)
                    {
                        NowTreeNode = Tnodes[0];
                        FindLevel++;
                    }
                    else
                    {
                        break;
                    }
                }
                if (NowTreeNode == null)
                {
                    NowTreeNode = new TreeNode(SettingPoints[0]);
                    NowTreeNode.Tag = item;
                    FindLevel++;
                    TViewSettingList.Nodes.Add(NowTreeNode);
                }
                int i;
                for (i = FindLevel; i < SettingPoints.Count(); i++)
                {
                    TreeNode tnode = new TreeNode(SettingPoints[i]);
                    tnode.Tag = item;
                    NowTreeNode.Nodes.Add(tnode);
                    NowTreeNode = tnode;
                }
            }
        }
        private void SettingWnd_Load(object sender, EventArgs e)
        {
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SettingPool.AcceptSetting();
            SettingPool.SaveSetting();
            Hide();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            SettingPool.ResetToDefault();
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }
        private bool IsInited = false;
        private void SettingWnd_Shown(object sender, EventArgs e)
        {
            if (!IsInited)
            {
                TViewSettingList.Nodes.Clear();
                panelSetting.Controls.Clear();
                InitSettingList();
                IsInited = true;
            }
            SettingPool.ResetEditor();
        }

        private void TViewSettingList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if(e.Node!=null)
            {
                if(e.Node.Tag!=null)
                {
                    SettingEditorItem item = (SettingEditorItem)e.Node.Tag;
                    SettingEditorBase Editor = SettingPool.GetEditor(item);
                    panelSetting.Controls.Add(Editor);
                    Editor.Dock = DockStyle.Fill;
                }
            }
        }

        private void TViewSettingList_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if(e.Node!=null)
            {
                if(panelSetting.Controls.Count>0)
                {
                    panelSetting.Controls.Clear();
                }
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            SettingPool.AcceptSetting();
        }
    }
}
